export const get = (key: string): string => {

    const reg = new RegExp(`(^| )${key}=([^;]*)(;|$)`);
    const result = reg.exec(document.cookie);
    if (result) {
        return result[2] || '';
    }

    return '';
};

export const set = (key: string, value: string, options: {
    expires?: number | Date;
    path?: string;
    domain?: string;
    secure?: boolean;
}): void => {
    let expires = options.expires;
    if (typeof options.expires === 'number') {
        expires = new Date();
        expires.setTime(expires.getTime() + options.expires); // 确保 expires 是以毫秒为单位
    } else if (expires instanceof Date) {
        // 如果 expires 已经是 Date 对象，则不需要修改
    } else {
        expires = undefined; // 如果没有提供 expires，则不设置过期时间
    }

    let cookieString = `${key}=${value}`;
    if (options.path) {
        cookieString += `; path=${options.path}`;
    }
    if (expires) {
        cookieString += `; expires=${expires.toUTCString()}`;
    }
    if (options.domain) {
        cookieString += `; domain=${options.domain}`;
    }
    if (options.secure) {
        cookieString += '; secure';
    }

    document.cookie = cookieString;
};

export const remove = (key: string): void => {
    set(key, '', {
        path: '/',
        expires: new Date(0)
    });
};